package com.banciyuan.bcywebview.utils.http;

import android.content.Context;

import com.banciyuan.bcywebview.base.myinterface.ActionInterface;
import com.banciyuan.bcywebview.base.myinterface.HelpActionInterface;
import com.banciyuan.bcywebview.base.view.toast.MyToast;

import org.json.JSONObject;

//用来处理statuscode的类，几乎所有的请求都会走这个类的方法，如果发现有没走的，请规范
public class StatusCodeUtils {

    public static Boolean dealStatusCode(String response, Context mContext) {
        return dealStatusCode(response, mContext, null);
    }

    //具体的处理方法：code为1就返回true，为140就调用回调的nodata，toast出返回的信息然后返回false，为其他就调用neterror，toast出返回的信息然后返回false
    public static Boolean dealStatusCode(String response, Context mContext, ActionInterface mInterface) {
        try {
            JSONObject result = new JSONObject(response);
            int status = result.getInt(HttpUtils.UPLOAD_STATUS);
            switch (status) {
                case 1:
                    return true;
                case 140:
                    if (mInterface != null) {
                        ((HelpActionInterface) mInterface).nodataError("1");
                    }
                    MyToast.show(mContext, result.getString(HttpUtils.UPLOAD_DATA));
                    return false;
                default:
                    if (mContext != null) {
                        MyToast.show(mContext, result.getString(HttpUtils.UPLOAD_DATA));
                    }
                    if (mInterface != null) {
                        mInterface.onNetError("");
                    }
                    return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
